package cn.afactory.tygz.ui.base.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.entity.CommunityInfo;
import cn.afactory.tygz.ui.base.utils.NaviUtils;

/**
 * Created by Kenneth on 15/9/14.
 */
public class CommunityGridAdapter extends RecyclerView.Adapter<CommunityGridAdapter.ViewHolder> {

    private Context mContext;
    private List<CommunityInfo> mCommunityInfoList = new ArrayList<CommunityInfo>();

    public CommunityGridAdapter(Context context, List<CommunityInfo> peopleInfoList ) {
        mContext = context;
        mCommunityInfoList = peopleInfoList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // Create a new view.
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.fg_community_grid_item, viewGroup, false);
        return new ViewHolder(mContext, v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        CommunityInfo communityInfo = mCommunityInfoList.get(i);
        viewHolder.getIconImageView().setImageResource(communityInfo.icon);
        viewHolder.getNameTextView().setText(communityInfo.name);
    }

    @Override
    public int getItemCount() {
        return mCommunityInfoList.size();
    }

    public void setmPeopleInfoList(List<CommunityInfo> mPeopleInfoList) {
        this.mCommunityInfoList = mPeopleInfoList;
        notifyDataSetChanged();
    }

    /**
     * Provide a reference to the type of views that you are using (custom ViewHolder)
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView iconImageView;
        private final TextView nameTextView;

        public ViewHolder(final Context context, View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int positon = getAdapterPosition();
                    switch (positon) {
                        case 0:
                            NaviUtils.gotoRestaurantListPage(context);
                            break;
                        case 1:
                            NaviUtils.gotoApartmentListPage(context);
                            break;
                        case 2:
                            NaviUtils.gotoSecondGoodsListPage(context);
                            break;
                        case 3:
                            NaviUtils.gotoEntertainmentListPage(context);
                            break;
                        case 4:
                            NaviUtils.gotoScenicSpotsListPage(context);
                            break;
                        case 5:
                            NaviUtils.gotoLifeServiceListPage(context);
                            break;
                        default:
                            break;
                    }
                }
            });
            nameTextView = (TextView)v.findViewById(R.id.community_name);
            iconImageView = (ImageView)v.findViewById(R.id.community_icon);
        }

        public ImageView getIconImageView() {
            return iconImageView;
        }

        public TextView getNameTextView() {
            return nameTextView;
        }
    }

}
